package iris.util
{
	public class StringProcessor
	{
		private var _string:String;
		private var _isEscalation:Boolean
		
		public function StringProcessor(string:String, isEscalation:Boolean = false)
		{
			_string = string;
			_isEscalation = isEscalation;
		}
		public function intercept(startTag:String, endTag:String, includeStartTag:Boolean, includeEndTag:Boolean):String
		{
			var result:String;
			
			// startIndex
			var startIndex:int;
			if(startTag == "")
				startIndex = 0;
			else
				startIndex = _string.indexOf(startTag);
			if(startIndex == -1)
				throw new Error("invalide startTag: " + startTag);
				
			// endIndex
			var endIndex:int;
			if(endTag == "")
				endIndex = _string.length;
			else
				endIndex = _string.indexOf(endTag, startIndex + startTag.length);
			if(endIndex == -1)
				throw new Error("invalide endIndex: " + endIndex);
			// intercept
			if(!includeStartTag)
				startIndex = startIndex + startTag.length;
			if(includeEndTag)
				endIndex = endIndex + endTag.length;

			result = _string.substring(startIndex, endIndex);
			
			if(_isEscalation)
				_string = result;
			return result;
		}
		public function drop(startTag:String, endTag:String, includeStartTag:Boolean, includeEndTag:Boolean, isLoop:Boolean):String
		{
			var result:String = _string;
			
			var isFirst:Boolean = true;
			while(isFirst || isLoop)
			{
				var string:String = result;
				// startIndex
				var startIndex:int;
				if(startTag == "")
					startIndex = 0;
				else
					startIndex = string.indexOf(startTag);
				if(startIndex == -1)
					break;
				// endIndex
				var endIndex:int;
				if(endTag == "")
					endIndex = string.length;
				else
					endIndex = string.indexOf(endTag, startIndex + startTag.length);
				if(endIndex == -1)
					break;
				// drop
				if(includeStartTag)
					result = string.substring(0, startIndex + startTag.length);
				else
					result = string.substring(0, startIndex);
					
				if(includeEndTag)
					result = result + string.substring(endIndex);
				else
					result = result + string.substring(endIndex + endTag.length);
					
				isFirst = false;
			}
			
			if(_isEscalation)
				_string = result;
			return result;
		}
		public function interceptNumber():String
		{
			var s:String = "";
			
			var seeked:Boolean = false;
			for(var i:int = 0; i < _string.length; i++)
			{
				var c:Number = _string.charCodeAt(i);
				// 0-9 .
				var isValid:Boolean = ((c >= 48 && c <= 57) || c == 46);
				
				if(isValid)
					s = s + String.fromCharCode(c);
				else if(s != "")
					break;
			}
			return s;
		}
		public function compareTo(s:String):int
		{
			var l:int = Math.min(_string.length, s.length);
			
			if(_string.substr(0, l) == s.substring(0, l))
			{
				return _string.length - s.length;
			}
			else
			{
				for(var i:int; i < l; i++)
				{
					var c1:int = _string.charCodeAt(i);
					var c2:int = s.charCodeAt(i);
					if(c1 == c2)
						continue;
					else
						return c1 - c2;
				}
			}
			return 0;
		}
		public function get source():String
		{
			return _string;
		}
	}
}